﻿using GetData;
using GetData.webProductClass;
using jutehui.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using HtmlAgilityPack;

namespace jutehui.GetData
{
    public class MeiDeBi
    {
        HtmlAgilityPack.HtmlDocument doc;
        public MeiDeBi()
        {
            doc = new HtmlAgilityPack.HtmlDocument();
        }

        /// <summary>
        /// 通过网页html获取商品信息并保存到数据库中
        /// </summary>
        /// <param name="url"></param>
        /// <param name="shoppingType">海淘2 精选1</param>
        /// <returns></returns> 返回更新数据条数
        public int GetProductsToDataBase(string url, int shoppingType, ref string msg)
        {
            var htmlText = CustomMethod.GetHtmlByHttpHelper(url);
            var pList = GetProductByRegex(htmlText, shoppingType, ref msg);
            if (pList == null)
            {
                return 0;
            }
            DataTable dt = CustomMethod.ListToDataTable<Product>(pList, "Product", ref msg);
            if (dt == null || dt.Rows.Count < 1)
            {
                return 0;
            }
            jutehui.BLL.Product bll = new jutehui.BLL.Product();
            //string msg = "";
            if (bll.SaveTableData("Product", dt, out msg))
            {
                return dt.Rows.Count;
            }
            else
            {
                msg = "没得比：GetProductsToDataBase" + msg;
                CustomMethod.WriteLog(msg);
                return 0;

            }
        }


        /// <summary>
        /// 根据html 正则解析出商品信息
        /// </summary>
        /// <param name="htmlText"></param>
        /// <param name="shoppingType">精选商品传1  海淘商品传2</param>
        /// <returns></returns>
        private List<Product> GetProductByRegex(string htmlText, int shoppingType, ref string msg)
        {
            List<Product> pList = new List<Product>();
            ProductTypeEnum pEnum = new ProductTypeEnum();
            //正则获取每个商品 标签内容
            doc.LoadHtml(htmlText);
            var area = doc.DocumentNode.SelectNodes("//div[@class='_body share-list-grid']");
            if (area == null || area.Count < 1)
            {
                //没有匹配到数据
                return null;
            }
            htmlText = area[0].InnerHtml;
            string regexStr = @"<li>[\s\S]*?</li>";//class=""item clearfix""
            if (htmlText != "" && htmlText != null)
            {
                var list = CustomMethod.GetInfoByRegex(regexStr, htmlText, 0);

                if (list == null || list.Count < 1)
                {
                    msg = "";// " GetProductByRegex，list.Count=0，尝试抓取网络数据失败";
                    CustomMethod.WriteLog("没得比：没有抓取到数据，请检查规则。" + DateTime.Now);
                    return null;
                }

                //遍历每一个商品标签内容 获取对象属性  ([^<]*)
                for (int index = 0; index < list.Count; index++)
                {
                    Product product = new Product();
                    try
                    {
                        product = GetProduct(list[index]);
                        if(product==null)
                        {
                            continue;
                        }
                        product.UpdateTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        product.UpdateTime = DateTime.Now;
                        product.Pname = System.Web.HttpUtility.HtmlDecode(product.Pname);// product.Pname.Replace("&#x2F;", "/").Replace("#039;", "'").Replace("&amp;", "&").Replace("&#x27;", "'").Replace("&quot;", "\""); 
                      
                    }
                    catch (Exception ex)
                    {
                        msg = "没得比：GetProductByRegex:" + ex.Message.ToString();
                        CustomMethod.WriteLog(msg);
                        continue;
                    }
                    pList.Add(product);
                }
                //按商品更新时间倒叙插入数据库
                var newList = CustomMethod.GetDistinctProdcuts(pList);
                newList.Reverse();
                return newList;
            }


            else
            {
                return null;
            }

        }
        /// <summary>
        ///根据规则获取属性值 一个商品item
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <returns></returns>
        public Product GetProduct(string htmlContent)
        {
            Product pro = new Product();
            pro.FromWeb = "没得比";
            pro.TypeId = 1;
            doc.LoadHtml(htmlContent);
            //名称
            var name = CustomMethod.GetValue(doc, "//div[@class='tit f14']");
            if (name == "")
            {
                return null;
            }
            pro.Pname = name;
            //价格
            var pric = CustomMethod.GetValue(doc, "//i[@class='num']");
            pro.Price = pric;
            var oldpric = CustomMethod.GetValue(doc, "//span[@class='old']");
            pro.PriceType = oldpric;
            //图片
            var imglis = CustomMethod.GetAttr(doc, htmlContent, "//a/div/span/img", "src");
            if (imglis != null && imglis.Count > 0)
            {
                pro.ImgUrl = imglis[0];
            }
            //数据源
            var source = CustomMethod.GetValue(doc, "//a[@class='gray9 fr site']");
            pro.Source = source;
            //图片
            var outlink = CustomMethod.GetAttr(doc, htmlContent, "//a[@class='go m-button button-primary']", "href");
            if (outlink != null && outlink.Count > 0)
            {
                string productUrl = "";
                string link = outlink[0];
                //商品链接 带渠道号
                pro.Plink = CustomMethod.GetTrueUrlFromMeidebi(link, ref productUrl);
                if (pro.Plink == "")
                {
                    if (productUrl == "")
                    {
                        CustomMethod.WriteLog("没得比：没有抓取到商品链接");
                      
                    }
                    return null;
                }
                //入库前匹配数据库是否存在该商品 商品来源是否重复标识
                pro.Memo = productUrl;
            }
            else
            {
                return null;
            }
            //详细信息
            var detaillink = CustomMethod.GetAttr(doc, htmlContent, "//a[@class='inner']", "href");
            if (detaillink != null && detaillink.Count > 0)
            {
                var detail = detaillink[0];
                string htmlDescription = CustomMethod.GetHtmlByHttpHelper(detail);
                doc.LoadHtml(htmlDescription);
                var des = CustomMethod.GetValue(doc, "//div[@class='dp-desc']");
                doc.LoadHtml(des);

                var item = doc.DocumentNode.SelectNodes("//div/p");
                string descr = "";
                if (item != null && item.Count > 0)
                {
                    for (int index = 0; index < item.Count-1; index++)
                    {
                        descr += "<p>"+item[index].InnerText + "</p><br/>";
                    }
                }
                if (descr == "")
                {
                    try
                    {
                        var des1 = doc.DocumentNode.SelectNodes("//div[@class='desc-con post-content']");//".//a[@href]"
                        if (des1 != null && des1.Count > 0)
                        {
                            for (int index = 0; index < des1.Count; index++)
                            {
                                string desStr = "";

                                int endIndex = des1[index].InnerText.IndexOf(".excel-goods");
                                if(endIndex>0)
                                {
                                    desStr = des1[index].InnerText.Substring(0, endIndex);
                                }
                                else
                                {
                                    desStr = des1[index].InnerText;
                                }
                                descr += desStr + "<br/>";
                            }
                        }
                    }
                    catch 
                    {

                    }
                }
                pro.Description = descr;
                //分类  
                doc.LoadHtml(htmlDescription);
                var conllectin = doc.DocumentNode.SelectNodes("//div[@class='dp-crumb gray9']/a");//".//a[@href]"
                if (conllectin != null)
                {
                    try
                    {
                        //doc.LoadHtml(conllectin[0].InnerHtml);
                        //var aStr = doc.DocumentNode.SelectNodes("//a");//".//a[@href]"
                        string typstr = conllectin[2].InnerHtml;
                        var tyid = ProductTypeEnum.GetProductType(typstr, typeof(ProductTypeEnum.mdbType));
                        pro.ProductTypeId = tyid;
                    }
                    catch
                    {
                        pro.ProductTypeId = 16;
                    }
                }
                else
                {
                    pro.ProductTypeId = 16;
                }
            }
            return pro;
        }

    }
}
